package cn.line5.login;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.line5.module.ModuleDaoImpl;
import cn.line5.module.ModuleFunctionPointVo;
import cn.line5.organization.PermissionVo;

/**
 * <h1>登录用户VO</h1>
 * <br/>
 * 
 * @author Liu,Xiaojun
 */
public class LoginUser {
	private static final String LOGIN_SESSION_NAME = "LSEAM_USER";

	private static Map<String, ModuleFunctionPointVo> allPoint = new HashMap<String, ModuleFunctionPointVo>();

	static {
		List<ModuleFunctionPointVo> list = new ModuleDaoImpl().getPointList();
		if (list != null && list.size() > 0)
			for (ModuleFunctionPointVo vo : list)
				allPoint.put(vo.getComponentName() + "." + vo.getMethodName(),
						vo);
	}

	/**
	 * 是否登录
	 */
	public static Boolean isLogin(HttpServletRequest request) {
		return request.getSession().getAttribute(LOGIN_SESSION_NAME) != null;
	}

	/**
	 * 获取登录用户
	 */
	public static LoginVo getLoginUser(HttpServletRequest request) {
		if (isLogin(request))
			return (LoginVo) request.getSession().getAttribute(
					LOGIN_SESSION_NAME);
		return null;
	}

	/**
	 * 获取登录用户ID
	 */
	public static String getLoginUserId(HttpServletRequest request) {
		if (getLoginUser(request) != null)
			return getLoginUser(request).getUserId();
		return null;
	}

	/**
	 * 获取登录用户模块权限
	 */
	public static Map<String, PermissionVo> getModulePermission(
			HttpServletRequest request) {
		if (getLoginUser(request) != null)
			return getLoginUser(request).getModuleAndFunctionPermission();
		return null;
	}

	/**
	 * 获取登录用户没有的功能点权限
	 */
	public static Map<String, ModuleFunctionPointVo> getPointPermission(
			HttpServletRequest request) {
		if (getLoginUser(request) != null)
			return getLoginUser(request).getPointPermission();
		return null;
	}

	/**
	 * 获取所有功能点
	 */
	public static Map<String, ModuleFunctionPointVo> getAllPoint() {
		return allPoint;
	}
	
	/**
	 * 写入登录用户session
	 */
	public static void setLoginSession(HttpServletRequest request, LoginVo vo){
		request.getSession().setAttribute(LOGIN_SESSION_NAME, vo);
	}
	
	/**
	 * 注销登录用户session
	 */
	public static void clearLoginSession(HttpServletRequest request){
		if(isLogin(request))
			request.getSession().removeAttribute(LOGIN_SESSION_NAME);
	}
}
